<?php

namespace Home\Controller;

//use Think\Controller;

class IndexController extends ExtendController {

    //显示栏目
    public function index(){
        $Subject = D('Subject');
        import('ORG.Util.Page'); // 导入分页类
        $count = $Subject->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数

        $show = $Page->show();     // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $Subject->order('id DESC')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->display(); // 输出模板
    }
   
    //添加
    public function add() {
        $Subject = M('Subject');
        $data['name'] = $_POST['name'];
        $data['image'] = $_POST['image_url'];
        $data['date'] =date('Y-m-d H:i:s');
        $result = $Subject->add($data);
       
        if ($result != false) {
            $this->redirect('index');
        } else {
            $this->error('非法操作!');
        }
      
    }
      //修改
     public function save() {
        if(!empty($_POST)){
            $Subject = M('Subject');
            $map['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['image'] = $_POST['image_url'];   
            $result = $Subject->where($map)->save($data);
            if ($result != false) {
                $this->redirect('index');
            } else {
                $this->error('非法操作!');
            }
        }else{
           $Subject = M('Subject');
            $data['id'] = $_GET['id'];
            $column = $Subject->where($data)->find();
            if ($column) {
                $this->assign('list',$column);
               $this->display('updata_subject');
               
            }else{
                $this->error('非法操作');
            }
    }
}
    
    //删除
    public function delete() {
        $Subject = M('Subject');
        $map['id'] = $_GET['id'];
        $result = $Subject->where($map)->delete();
        if ($result != false) {
            $this->redirect('Home/Index/index');
        } else {
            $this->error('删除失败!');
        }
    }

}